package com.kele.oplog.service;

import com.kele.common.model.response.Result;
import com.kele.model.oplog.Remark;
import com.kele.oplog.dao.RemarkRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RemarkService {

    @Autowired
    RemarkRepository remarkRepository;

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    RedisTemplate redisTemplate;

    public List<Remark> findAll(Remark remark){
        return remarkRepository.findAll();
    }

    public Result insert(Remark remark){
        //1 操作Mongodb 保存一个菜品的评价
        remarkRepository.insert(remark);//万一保存失败怎么办？
        //2. Redis中评价+1
        Integer foodId = remark.getFoodId();
       /* Food food = new Food();//通过FoodId 从数据库查询
        food.setTitle("豆腐脑");
        food.setIntroduce("");*/
        double scores = redisTemplate.opsForZSet().incrementScore("foods", foodId, 1);
        System.out.println(scores);
        if(scores > 10 ){
            //Mysql 操作  top 置顶 加精 设置 1
            System.out.println("加精");//top=1
            System.out.println("修改时间更新");//
        }
        return Result.SUCCESS();
    }
}
